﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.admin.product
{
    public partial class product_auction_edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
               
                if (ChangeHope.WebPage.PageRequest.GetInt("id") > 0)
                {
                    BandInfo(ChangeHope.WebPage.PageRequest.GetInt("id"));
                }
                InitWebControl();
                if (ViewState["ID"] == null)
                {
                    YXShop.Common.PromptInfo.Popedom("002002002", "对不起，您没有权限进行添加");
                }
                else
                {
                    YXShop.Common.PromptInfo.Popedom("002002004", "对不起，您没有权限进行编辑");
                }
            }
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Save();
        }
        /// <summary>
        /// 验证
        /// </summary>
        private void InitWebControl()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            this.txtProductName.Attributes.Add("readonly", "readonly");
            this.txtProductName.Attributes.Add("onclick", "selectFile('AuctionProduct',new Array(" + this.hfid.ClientID + "," + this.txtProductName.ClientID + "),310,550,'" + sp.DummyPaht + "');");
            ChangeHope.WebPage.WebControl.SetDate(this.txtEndTime);
            ChangeHope.WebPage.WebControl.SetDate(this.txtStartTime);
            ChangeHope.WebPage.WebControl.Validate(this.txtAuctionName, "输入拍卖名称", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtProductName, "输入拍卖商品的名称", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtStartTime, "输入拍卖的开始时间", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtEndTime, "输入拍卖的结束时间", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtStartPrice, "输入起拍价,将作为商品拍卖的起始价格", "isfloat", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtAddPrice, "输入加价幅度,将作为商品拍卖的从起始价格加价标准", "isfloat", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtDeposit, "输入保证金", "isfloat", "必填", "该项为必填项");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
            this.txtProductName.Attributes.Add("readonly", "readonly");
        }
        protected void BandInfo(int id)
        {
            YXShop.BLL.Product.ProductAuction bll = new YXShop.BLL.Product.ProductAuction();
            YXShop.Model.Product.ProductAuction model = bll.GetModelID(id);
            this.txtAuctionName.Text = model.AuctionName;
            this.txtProductName.Text = model.ProductName;
            this.hfid.Value = model.ProductID.ToString();
            this.txtStartTime.Text = model.StartTime.ToString();
            this.txtEndTime.Text = model.EndTime.ToString();
            this.txtStartPrice.Text = model.Price.ToString();
            this.txtAddPrice.Text = model.PriceRange.ToString();
            this.txtDeposit.Text = model.Deposit.ToString();
            this.txtDescription.Text = model.Description;
            ViewState["PutoutID"] = model.PutoutID;
            ViewState["PutoutTypeID"] = model.PutoutTypeID;
            ViewState["ID"] = model.ID;
        }
        /// <summary>
        /// 获取分类名称
        /// </summary>
        /// <param name="strId"></param>
        /// <returns></returns>
        protected string ProductClassName(string strId)
        {
            string reStr = string.Empty;
            if (!string.IsNullOrEmpty(strId))
            {
                YXShop.BLL.Product.Productclass dll = new YXShop.BLL.Product.Productclass();
                DataTable dt = dll.GetMoreThanClassName(strId);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (!string.IsNullOrEmpty(reStr))
                    {
                        reStr = reStr + "," + dt.Rows[i]["name"].ToString();
                    }
                    else
                    {
                        reStr = dt.Rows[i]["name"].ToString();
                    }
                }
            }
            return reStr;
        }
        /// <summary>
        /// 保存信息
        /// </summary>
        protected void Save()
        {
            YXShop.BLL.Product.ProductAuction bll = new YXShop.BLL.Product.ProductAuction();
            YXShop.Model.Product.ProductAuction model = new YXShop.Model.Product.ProductAuction();
            model.AuctionName = this.txtAuctionName.Text;
            model.Description = this.txtDescription.Text;
            model.ProductID =int.Parse(this.hfid.Value);
            model.ProductName = this.txtProductName.Text;
            /**
            * kevin
            * 修改时间：09.12.28
            */
            if (!ChangeHope.Common.ValidateHelper.IsMoney(txtStartPrice.Text) || !ChangeHope.Common.ValidateHelper.IsMoney(txtAddPrice.Text) || !ChangeHope.Common.ValidateHelper.IsMoney(txtDeposit.Text))
            {
                this.ltlMsg.Text = "操作失败，请填写正确金额。";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }
            if(float.Parse(txtStartPrice.Text) <= 0|| float.Parse(txtAddPrice.Text)<= 0||float.Parse(txtDeposit.Text)<=0)
            {
                this.ltlMsg.Text = "操作失败，金额小于等于0。";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }
            if (Convert.ToDateTime(txtStartTime.Text) < Convert.ToDateTime(System.DateTime.Now.ToShortTimeString()).AddDays(-1))
            {
                this.ltlMsg.Text = "操作失败，起拍时间小于当前时间。";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }
            if (Convert.ToDateTime(txtStartTime.Text) > Convert.ToDateTime(txtEndTime.Text))
            {
                this.ltlMsg.Text = "操作失败，结束时间小于起拍时间。";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }
            model.StartTime = Convert.ToDateTime(this.txtStartTime.Text);
            model.EndTime = Convert.ToDateTime(this.txtEndTime.Text);
            model.Price = decimal.Parse(this.txtStartPrice.Text);
            model.PriceRange = decimal.Parse(this.txtAddPrice.Text);
            model.Deposit = decimal.Parse(this.txtDeposit.Text);

            if (ViewState["ID"] == null)
            {
                model.PutoutID = 0;
                model.PutoutTypeID = 0;
                if (bll.Add(model) != 0)
                {
                    this.txtProductName.Text = string.Empty;
                    this.ltlMsg.Text = "操作成功，添加信息保存成功。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionOk";
                }
            }
            else
            {
                model.ID = Convert.ToInt32(ViewState["ID"].ToString());
                model.PutoutID = Convert.ToInt32(ViewState["PutoutID"].ToString());
                model.PutoutTypeID = Convert.ToInt32(ViewState["PutoutTypeID"].ToString());
                if (bll.Update(model) != 0)
                {
                    this.txtProductName.Text = string.Empty;
                    this.ltlMsg.Text = "操作成功，添加信息保存成功。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionOk";
                }
            }
        }
    }
}
